
package com.tools.cleanmaster.ui.card;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

import com.tools.ad.EntranceType;
import com.tools.ad.util.GARecordUtils;
import com.tools.cleanmaster.CleanApplication;
import com.tools.cleanmaster.CleanGAConstant;
import com.tools.cleanmaster.R;
import com.tools.cleanmaster.manager.RateManager;
import com.tools.cleanmaster.store.GlobalConfigSharedPrefManager;
import com.tools.cleanmaster.store.ResultPageSharedPrefManager;
import com.tools.cleanmaster.ui.card.animator.RateRecommendWindow;
import com.tools.cleanmaster.ui.card.factory.CardListAdapter;
import com.tools.cleanmaster.ui.card.viewholder.ACardViewHolder;
import com.tools.cleanmaster.ui.card.viewholder.RateCardViewHolder;
import com.tools.cleanmaster.ui.view.ToastHelper;
import com.tools.cleanmaster.utils.NetworkUtils;
import com.tools.cleanmaster.utils.StringUtils;

@SuppressWarnings("static-access")
public class RateItem extends ACardItem {

    private RateCardViewHolder mCardViewHolder;

    private boolean mHasStartAnim;

    private boolean mFirstShow = true;

    public RateItem(EntranceType entranceType, int pos) {
        super(entranceType, pos);
    }

    @Override
    public CardViewType getCardViewType() {
        return CardViewType.RATE;
    }

    @Override
    public boolean needShow(EntranceType entranceType) {
        return imContext.getADModule().isOrganicOrOtherChannel() && !ResultPageSharedPrefManager.getRatePress(imContext);
    }

    @Override
    public void bindCardView(final Activity activity, ACardViewHolder viewHolder, CardListAdapter cardAdapter, final int position) {
        super.bindCardView(activity, viewHolder, cardAdapter, position);
        mCardViewHolder = (RateCardViewHolder) viewHolder;
        mCardViewHolder.ratingContentMem.setText(Html.fromHtml(activity.getString(R.string.rate_cars_text, StringUtils.getSizeString(GlobalConfigSharedPrefManager.getTrashCleanTotal(CleanApplication.getInstance())))));
        mCardViewHolder.rateButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                doClick(activity, position);
            }
        });
        mCardViewHolder.mItemView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                doClick(activity, position);
            }
        });
    }

    @Override
    public String getCardName() {
        return CardType.RATE.key;
    }

    private void doClick(Activity activity, int position) {
        GARecordUtils.report(imContext, CleanGAConstant.C_FUNC, CleanGAConstant.A_CLICK,
                CleanGAConstant.L_CARD_RATING);

        if (!NetworkUtils.isNetworkAvaialble(activity)) {
            ToastHelper.create(CleanApplication.getInstance(), R.string.recommend_network_message, Toast.LENGTH_LONG).show();
            return;
        }

        try {
            ResultPageSharedPrefManager.setRatePress(CleanApplication.getInstance());
            activity.startActivity(RateManager.getRateIntent(activity));
            LocalBroadcastManager.getInstance(activity).sendBroadcast(new Intent(RateManager.GO_RATE));
            RateRecommendWindow.getsInstance().addView();
        } catch (Exception e) {
            Toast.makeText(activity, R.string.mark_dialog_market_not_found, Toast.LENGTH_SHORT).show();
        }
    }

    public void startRateAnim() {
        /*if (mFirstShow && mCardViewHolder != null && mCardViewHolder.ratingStarView != null) {
            mCardViewHolder.ratingStarView.doAnimation(true);
            mFirstShow = false;
        } else {
            if (mCardViewHolder != null && mCardViewHolder.ratingStarView != null) {
                mCardViewHolder.ratingStarView.showStars();
            }
        }*/
    }

    public boolean isStartAnim() {
        return mHasStartAnim;
    }
}
